import Vue from 'vue';
import VueRouter from 'vue-router';
import { constantRouterMap } from '@/config/router.config';

// hack router push/replace callback
['push', 'replace'].map((key) => ({
    k: key,
    prop: VueRouter.prototype[key]
  })).forEach((item) => {
  VueRouter.prototype[item.k] = function newCall(location, onResolve, onReject) {
    if (onResolve || onReject) return item.prop.call(this, location, onResolve, onReject);
    return item.prop.call(this, location).catch((err) => err);
  };
});

const router = new VueRouter({
  mode: 'history',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRouterMap
});

Vue.use(VueRouter);

export default router;
